function SaveFolders(folderPaths, runParams, redoFileload, doControls,conn)

fileCol = 2 + doControls;

fields = {'baseline_Threshold', ...
    'num_ClusterFFT_coef', ...
    'num_peakFFT_coef', ...
    'minimum_Width', ...
    'clusterSize', ...
    'minimum_FFT_Size', ...
    'lowPass_Freq', ...
    'minimum_cluster_FFT_Size'};

field = fields{1};
whereSQL = ['WHERE Round(' field ',4)=Round(' num2str( runParams.(field)) ',4)'];
for I=2:length(fields)
    field = fields{I};
    whereSQL=[whereSQL ' AND Round(' field ',4)=Round(' num2str( runParams.(field)) ',4)']; %#ok<AGROW>
end

sql =['select folders.Folder_Index, folders.Folder from folders ' whereSQL ';'];

ret = exec(conn,sql);
ret = fetch(ret);

if isempty(ret.Message)==false || isstruct(ret.Data)==false
    DoFolders = folderPaths;
else
    FolderNames=(ret.Data.Folder);
    cc=1;
    alreadyDone=[];
    for I=1:size(folderPaths,1)
        for J=1:size(FolderNames)
            a=folderPaths{I,fileCol};
            a=strrep(a,'\','/');
            if strcmp(a,char(FolderNames{J}))
                alreadyDone(cc)=I;
                cc=cc+1;
            end
        end
    end
    rows=1:size(folderPaths,1);
    rows(alreadyDone)=[];
    DoFolders = folderPaths(rows,:);
end
sql = 'SELECT max(Folder_Index) as m from folders;';
ret = exec(conn,sql);
ret = fetch(ret);

try
    if isnan(ret.Data.m)
        folder_Index=1;
    else
        if isempty(ret.Message)==false  || isstruct(ret.Data)==false
            folder_Index=1;
        else
            folder_Index=ret.Data.m+1;
        end
    end
catch
    folder_Index=1;
end

for I=1:size(DoFolders,1)
    folderInfo=DoFolders{I,fileCol}
    a=strrep(folderInfo,'\','/');
    
    sql ='INSERT INTO folders (folder_Index,Folder, number_Samples, numPeaks,numWaterPeaks,numClusters,avgBaselineVariance';
    
    for J=1:length(fields)
        field = fields{J};
        sql=[sql ',' field ]; %#ok<AGROW>
    end
    
    sql = [sql  ') VALUES (' num2str(folder_Index) ',''' a ''',0,0,0,0,0' ]; %#ok<AGROW>
    for J=1:length(fields)
        field = fields{J};
        sql=[sql ',' num2str( runParams.(field)) ]; %#ok<AGROW>
    end
    sql=[sql ');']; %#ok<AGROW>
    cur= exec(conn,sql)
    
    
    
    [folderNSamples folderNPeaks folderNClusters folderBaseLine]= FeatureCreation(conn, folder_Index,  folderInfo , runParams, false );
    
    
    sql =['Update folders SET number_Samples=' num2str(folderNSamples) ', '...
          'numPeaks=' num2str(folderNPeaks) ', ' ... 
          'numClusters=' num2str(folderNClusters) ', ' ...
          'avgBaselineVariance=' num2str(folderBaseLine) ' where folder_Index=' num2str(folder_Index)  ';' ];
    
    cur= exec(conn,sql);
   
    
    folder_Index=folder_Index+1;
end





return;


